package com.cjm.one.common.aop;

import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.cjm.one.common.annotation.IgnoreTenantForAdmin;
import com.cjm.one.common.utils.AuthSysUserContextHolder;
import com.cjm.one.model.system.entity.SysUserInfo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Arrays;
import java.util.Set;

@Aspect
@Component
@Slf4j
   public class AdminTenantAspect {

       public static final String ADMIN_KEY = "SUPER_ROLE_ADMIN";  //角色key
        private static final Set<String> ADMIN_PATHS = Set.of("/admin/", "/manager/");
       @Around("execution(* com.cjm.one..*.*(..)) &&@annotation(ignoreTenantForAdmin)")
       public Object aroundAdminMethod(ProceedingJoinPoint joinPoint, IgnoreTenantForAdmin ignoreTenantForAdmin) throws Throwable {
           try {
               MethodSignature signature = (MethodSignature) joinPoint.getSignature();
               log.info("AdminTenantAspect 开始执行方法: {}", signature.getName());

               ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
               HttpServletRequest request = attributes.getRequest();
               String requestURI = request.getRequestURI();

               // 登录路径直接忽略租户
               boolean isLoginPath = requestURI.contains("/system/sysUser/name/") || requestURI.contains("/auth/system/findUserPermsList/");
               if (isLoginPath) {
                   InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
                   return joinPoint.proceed();
               }

               // 仅当路径包含 /admin 时校验管理员权限
               boolean isAdminPath = requestURI.contains("/admin");
               if (isAdminPath) {
                   SysUserInfo authContextSysUser = AuthSysUserContextHolder.getAuthContextSysUser();
                   if (authContextSysUser != null) {
                       boolean isAdmin = authContextSysUser.getRoleList().stream()
                               .anyMatch(role -> ADMIN_KEY.equals(role.getRoleKey()));
                       log.info("当前用户是否是总管理员: {}", isAdmin);

                       if (isAdmin) {
                           // 管理员忽略租户
                           InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
                       }
                   }
               }

               return joinPoint.proceed();



           }catch (Exception e){
               e.printStackTrace();
           }finally {
               // 清除忽略策略
               InterceptorIgnoreHelper.clearIgnoreStrategy();
           }

           return joinPoint.proceed();
       }


   }